import $ from 'jquery'

import Vue from 'vue'

import VueRouter from 'vue-router'
Vue.use(VueRouter)

import VueJsonp from 'vue-jsonp'
Vue.use(VueJsonp)
import VuePreview from 'vue-preview'
Vue.use(VuePreview)
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
// import {Header,Button} from 'mint-ui'
// Vue.component(Header.name,Header);
// Vue.component(Button.name,Button);

import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'
import app from './app.vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import router from "./router.js"

var car= JSON.parse(localStorage.getItem('car')||'[]')
var store = new Vuex.Store({
    state:{//this.$store.state.***
        car:car
    },
    mutations:{//this.$store.commit('方法名称','按需传递唯一的参数')
        addToCar(state,goodsinfo){

            var flag=false;
            state.car.some(item=>{
                if(item.id==goodsinfo.id){
                    item.count+=parseInt(goodsinfo.count);
                    flag=true
                    return true 
                }
            })
            if(!flag){
                state.car.push(goodsinfo)
            }
            localStorage.setItem('car',window.JSON.stringify(state.car))
        },
        updateGoodsInfo(state,goodsinfo){
            state.car.some(item=>{
                if(item.id==goodsinfo.id){
                    item.count=parseInt(goodsinfo.count)
                    return true
                }
            })
            localStorage.setItem('car',window.JSON.stringify(state.car))
       },
       updateSelected(state,info){
           state.car.some(item=>{
               if(item.id==info.id){
                   item.selected=info.selected
               }
           })
           localStorage.setItem('car',window.JSON.stringify(state.car))
       }
    },
    getters:{//this.$store.getters.***
       getAllCount(state){
           var c = 0;
           state.car.forEach(item=>{
               c+=item.count
           })
           return c;  
       },
       getGoodsCount(state){
           var o={};
           state.car.forEach(item=>{
               o[item.id]=item.count
           })
           return o;
       },
       getGoodsSelected(state){
           var o={}
           state.car.forEach(item=>{
               o[item.id]=item.selected
           })
           return o
       } 
    }
})
var vm = new Vue({
    el:'#app',
    data:{},
    methods:{},
    render:c=>c(app),
    router,
    store
})